﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion
{
    public class LoadNode : NodeBase
    {
        public IEnumerable<string> GameItemCollection { get { return gameItemDictionary.Keys; } }

        private Dictionary<string, List<GameData>> gameItemDictionary;

        public LoadNode(string name) : base()
        {
            base.Name = name;
            gameItemDictionary = new Dictionary<string, List<GameData>>();
        }

        public override NodeBase Clone()
        {
            LoadNode loadNode = new LoadNode(Name);
            base.InsertParentContent(loadNode);

            foreach (KeyValuePair<string, List<GameData>> keyValuePair in gameItemDictionary)
            {
                loadNode.AddGameItem(keyValuePair.Key);

                foreach (GameData gameToolData in keyValuePair.Value)
                    loadNode.AddGameTool(keyValuePair.Key, gameToolData.GameToolName, gameToolData.Clone());
            }

            return loadNode;
        }

        internal override void UpdateGameToolData(GameItem gameItem)
        {
            foreach (string gameItemName in GameItemCollection)
                LoadGameItem(gameItemName);

            foreach (KeyValuePair<string, List<GameData>> keyValuePair in gameItemDictionary)
            {
                foreach (GameData gameData in keyValuePair.Value)
                    gameData.UpdateGameData(null);
            }

            base.UpdateGameToolData(gameItem);
        }

        public void LoadGameItem(string gameItemName)
        {
            GameItemManager.Instance.CreateGameItem(gameItemName);
        }

        public IEnumerable<GameData> ObtainGameTools(string gameItemName)
        {
            return gameItemDictionary[gameItemName];
        }

        public GameData ObtainGameData(string gameItemName, string gameToolName)
        {
            return gameItemDictionary[gameItemName].First(x => x.GameToolName == gameToolName);
        }

        public void AddGameItem(string gameItemName)
        {
            gameItemDictionary.Add(gameItemName, new List<GameData>());
        }

        public void RemoveGameItem(string gameItemName)
        {
            gameItemDictionary.Remove(gameItemName);
        }

        public void RenameGameItem(string oldGameItemName, string newGameItemName)
        {
            List<GameData> gameDataList = gameItemDictionary[oldGameItemName];

            gameItemDictionary.Remove(oldGameItemName);

            foreach (GameData gameData in gameDataList)
                gameData.GameItemName = newGameItemName;
            
            gameItemDictionary.Add(newGameItemName, gameDataList);
        }

        public void AddGameTool(string gameItemName, string gameToolName, GameData gameData)
        {
            gameData.GameItemName = gameItemName;
            gameData.GameToolName = gameToolName;
            gameItemDictionary[gameItemName].Add(gameData);
        }

        public GameData AddGameTool(string gameItemName, string gameToolName)
        {
            GameData gameData = new GameData() { GameItemName = gameItemName, GameToolName = gameToolName };
            gameItemDictionary[gameItemName].Add(gameData);
            return gameData;
        }

        public void RemoveGameTool(string gameItemName, string gameToolName)
        {
            gameItemDictionary[gameItemName].Remove(ObtainGameData(gameItemName, gameToolName));
        }
    }
}
